﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Collections.Generic;
using System.Data;

public partial class ProductDetail3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //int ma = int.Parse(Request.QueryString["masp"]);
        if (!IsPostBack)
        {
            this.getma(2);
        }

    }
    public void getma(int masp)
    {
        rptHinhLon.DataSource = HinhAnhBLL.loadhinh(masp);
        rptHinhLon.DataBind();
        rptHinhNho.DataSource = HinhAnhBLL.loadhinh(masp);
        rptHinhNho.DataBind();

       
        Dtl_motaSp.DataSource = SanPhamBLL.laythongtinSP(masp);
        Dtl_motaSp.DataKeyField = "Masanpham";
        Dtl_motaSp.DataBind();

        SanPhamBLL.Update_luotxem(masp);
        luusession(masp);
        
    }

    public void luusession(int masp)
    {
        San_pham Bang_sp = (San_pham)Session["san_pham"];

        DataRow dong = Bang_sp.Rows.Find(masp);
        if (dong != null)
        {
            return;
        }

        else
        {
            DataTable bang = SanPhamBLL.Sesssion_luu(masp);
            if (bang.Rows.Count > 0)
            {
                DataRow dong_moi = Bang_sp.NewRow();
                dong_moi["Masanpham"] = masp;
                dong_moi["tensanpham"]=bang.Rows[0]["Tensanpham"];
                dong_moi["Giaban"] = bang.Rows[0]["Giaban"];
                dong_moi["Hinhthunho"] = bang.Rows[0]["Hinhthunho"];
                Bang_sp.Rows.Add(dong_moi);
            }

        }
        Bang_sp.AcceptChanges();
        Session["san_pham"] = Bang_sp;
    }


    protected void Dtl_motaSp_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "chon_mua")
        {
            //ShoppingCart giohang = new ShoppingCart();
            int masp =(int) Dtl_motaSp.DataKeys[e.Item.ItemIndex];

           ShoppingCart.Instance.AddItem1(masp);




            //var giohang = (Gio_Hang)Session["giohang"];
            //int masp =(int) Dtl_motaSp.DataKeys[e.Item.ItemIndex];
            //var tensp = (Label)e.Item.FindControl("tenSP");
            //DataRow dong = giohang.Rows.Find(masp);
            //if (dong != null)
            //{
            //    int soluong = (int)dong["so_luong"];
            //    dong["so_luong"] = soluong + 1;
            //}
            //else
            //{
            //    DataTable bang = SanPhamBLL.Sesssion_luu(masp);
                
                
            //    DataRow dong_moi = giohang.NewRow();
            //    dong_moi["Masanpham"] = masp;
            //    dong_moi["tensanpham"] = bang.Rows[0]["Tensanpham"];
            //    dong_moi["Giaban"] = bang.Rows[0]["Giaban"];
            //    dong_moi["Hinhthunho"] = bang.Rows[0]["Hinhthunho"];
            //    dong_moi["so_luong"] = 1;
            //    dong_moi["giaban"] = double.Parse(e.CommandArgument.ToString());

            //    //dong_moi["masanpham"] = masp;
            //    //dong_moi["tensanpham"] = (string)(tensp.Text);
            //    //dong_moi["so_luong"] = 1;
            //    //dong_moi["giaban"] = double.Parse(e.CommandArgument.ToString());


            //    giohang.Rows.Add(dong_moi);
                //giohang.AcceptChanges();
               // Session["ASPNETShoppingCart"] = giohang;
            }
            
            
        }
    }
